package org.jeecg.modules.device.quartzJob;

import com.alibaba.fastjson.JSONArray;
import com.zhouwr.common.message.SendMessage;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.modules.device.service.IDeviceFunctionService;
import org.jeecg.modules.device.service.IDeviceInstanceService;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @program: jeecg-boot-module-iot
 * @description: 执行实例功能，需调用网络组件，发送请求
 * @author: zhouwr
 * @create: 2020-05-23 11:29
 * @version：1.0
 **/
@Component
@Slf4j
public class InvokeFunctionJob implements Job {
    @Autowired
    private IDeviceInstanceService instanceService;
    @Autowired
    private IDeviceFunctionService deviceFunctionsService;

    /**
     * 若参数变量名修改 QuartzJobController中也需对应修改
     */
    @Setter
    private String sendMessages;

    @Override
    public void execute(JobExecutionContext context) throws JobExecutionException {

        // 网络连接
        try {
            JSONArray.parseArray(sendMessages, SendMessage.class)
                    .forEach(sendMessage -> {
                        deviceFunctionsService.invokeFunctionAsync(sendMessage);
                    });
        } catch (Exception e) {
            log.error(e.getMessage());
//            e.printStackTrace();
        }
    }

}
